<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>退票规则定义管理</title>
<meta name="decorator" content="default" />
<script type="text/javascript">
		$(document).ready(function() {
			$("#rtnrulename").focus();
			$("#inputForm").validate({
				rules: {
					rtnrulename: {remote: "${ctx}/returnrule/tReturnrule/checkRtnrulename?oldRtnrulename=" + encodeURIComponent('${tReturnrule.rtnrulename}')}
			    },
			    messages:{
			    	rtnrulename: {remote: "退票规则名称已存在"}
			    },
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		function addRow(list, idx, tpl, row){
			var data = {
				idx: idx, delBtn: true, row: row
			};
			if(typeof(row) == 'undefined'){
				data = {
					idx: idx, delBtn: true, row: { rtndtlid:'',rtnorder:'',rtnpoint:'',rtnfrom:'',rtnto:'',rtntimetype:'',rtnamount:'',rtnamttype:''}
				};
			}
			$(list).append(template(tpl, data));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
		}
		
		function delRow(obj, prefix){
			$(prefix).remove();
			/* var id = $(prefix+"_id");
			
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			} */
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/returnrule/tReturnrule/">退票规则定义列表</a></li>
		<li class="active"><a
			href="${ctx}/returnrule/tReturnrule/form?returnid=${tReturnrule.returnid}">退票规则定义<shiro:hasPermission
					name="returnrule:tReturnrule:edit">${not empty tReturnrule.returnid?'修改':'添加'}</shiro:hasPermission>
				<shiro:lacksPermission name="returnrule:tReturnrule:edit">查看</shiro:lacksPermission></a></li>
	</ul>
	<br />
	<form:form id="inputForm" modelAttribute="tReturnrule"
		action="${ctx}/returnrule/tReturnrule/save" method="post"
		class="form-horizontal">
		<form:hidden path="returnid" />
		<sys:message content="${message}" />
		<div class="control-group">
			<label class="control-label">规则名称：</label>
			<div class="controls">
				<form:input path="rtnrulename" htmlEscape="false"
					class="input-xlarge required" />
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">退票规则明细表：</label>
			<div class="controls">
				<table id="contentTable"
					class="table table-striped table-bordered table-condensed">
					<thead>
						<tr>
							<th class="hide"></th>
							<th>退票时间点</th>
							<th>退票时间起点</th>
							<th>退票时间终点</th>
							<th>退票时间单位</th>
							<th>退票金额</th>
							<th>退票金额类型</th>
							<shiro:hasPermission name="returnrule:tReturnrule:edit">
								<th width="10">&nbsp;</th>
							</shiro:hasPermission>
						</tr>
					</thead>
					<tbody id="tReturnruleDtlList">
					</tbody>
					<shiro:hasPermission name="returnrule:tReturnrule:edit">
						<tfoot>
							<tr>
								<td colspan="10"><a href="javascript:"
									onclick="addRow('#tReturnruleDtlList', tReturnruleDtlRowIdx, tReturnruleDtlTpl);tReturnruleDtlRowIdx = tReturnruleDtlRowIdx + 1;"
									class="btn">新增</a></td>
							</tr>
						</tfoot>
					</shiro:hasPermission>
				</table>
				<script type="text/template" id="tReturnruleDtlTpl">
						<tr id="tReturnruleDtlList{{idx}}">
							<td class="hide">
								<input id="tReturnruleDtlList{{idx}}_delFlag" name="tReturnruleDtlList[{{idx}}].delFlag" type="hidden" value="0"/>
								<input id="tReturnruleDtlList{{idx}}_rtndtlid" name="tReturnruleDtlList[{{idx}}].rtndtlid" type="hidden" value="{{row.rtndtlid}}"/>
							</td>
							<td>
								<select id="tReturnruleDtlList{{idx}}_rtnpoint" name="tReturnruleDtlList[{{idx}}].rtnpoint" data-value="{{row.rtnpoint}}" class="input-small required">
									<option value=""></option>
									<c:forEach items="${fns:getDictList('rtnpoint')}" var="dict">
										<option value="${dict.value}">${dict.label}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="tReturnruleDtlList{{idx}}_rtnfrom" name="tReturnruleDtlList[{{idx}}].rtnfrom" type="text" value="{{row.rtnfrom}}" class="input-small required digits"/>
							</td>
							<td>
								<input id="tReturnruleDtlList{{idx}}_rtnto" name="tReturnruleDtlList[{{idx}}].rtnto" type="text" value="{{row.rtnto}}" class="input-small required digits"/>
							</td>
							<td>
								<select id="tReturnruleDtlList{{idx}}_rtntimetype" name="tReturnruleDtlList[{{idx}}].rtntimetype" data-value="{{row.rtntimetype}}" class="input-small required">
									<option value=""></option>
									<c:forEach items="${fns:getDictList('timetype')}" var="dict">
										<option value="${dict.value}">${dict.label}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="tReturnruleDtlList{{idx}}_rtnamount" name="tReturnruleDtlList[{{idx}}].rtnamount" type="text" value="{{row.rtnamount}}" class="input-small required number"/>
							</td>
							<td>
								<select id="tReturnruleDtlList{{idx}}_rtnamttype" name="tReturnruleDtlList[{{idx}}].rtnamttype" data-value="{{row.rtnamttype}}" class="input-small required">
									<option value=""></option>
									<c:forEach items="${fns:getDictList('rtnamttype')}" var="dict">
										<option value="${dict.value}">${dict.label}</option>
									</c:forEach>
								</select>
							</td>
							<shiro:hasPermission name="returnrule:tReturnrule:edit"><td class="text-center" width="10">
								{{if delBtn}}<span class="close" onclick="delRow(this, '#tReturnruleDtlList{{idx}}')" title="删除">&times;</span>{{/if}}
							</td></shiro:hasPermission>
						</tr>
					</script>
				<script type="text/javascript">
						var tReturnruleDtlRowIdx = 0, tReturnruleDtlTpl = 'tReturnruleDtlTpl';
						$(document).ready(function() {
							var data = ${fns:toJson(tReturnrule.tReturnruleDtlList)};
							for (var i=0; i<data.length; i++){
								addRow('#tReturnruleDtlList', tReturnruleDtlRowIdx, tReturnruleDtlTpl, data[i]);
								tReturnruleDtlRowIdx = tReturnruleDtlRowIdx + 1;
							}
						});
					</script>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="returnrule:tReturnrule:edit">
				<input id="btnSubmit" class="btn btn-primary" type="submit"
					value="保 存" />&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回"
				onclick="history.go(-1)" />
		</div>
	</form:form>
</body>
</html>